package com.paranoidvulture.javaEngine.MainPackage;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

import com.paranoidvulture.javaEngine.Utilities.Camera2D;
import com.paranoidvulture.javaEngine.Views.EditorMenu;
import com.paranoidvulture.javaEngine.Views.Menu;
import com.paranoidvulture.javaEngine.Views.PlayableView;
import com.paranoidvulture.javaEngine.Views.ViewHandler;


public class MainGame extends Core
{
	public static void main(String[] args){
		MainGame mg = new MainGame();
		mg.run();
	}
	
	
	ViewHandler views;
	Menu men;
	PlayableView gameTime;
	EditorMenu editMenu;
	String sIndex = "";
	String[] menuItems = {
		"Singleplayer",
		"Editors",
		"Multiplayer",
		"Options",
		"Exit"
	};
	Camera2D cam;
	
	public void init(){
		super.init();
		cam = new Camera2D(s);
		views = new ViewHandler();
		
		men = new Menu(s, menuItems);
		men.init();
		men.setEnabled(true);
		men.setName("MainMenu");
		
		gameTime = new PlayableView(s);
		gameTime.setName("SinglePlayer");
		
		editMenu = new EditorMenu(s);
		
		views.addToList(men);
	}

	//figure out how nested each view is.
	public void update(long timePassed)
	{
		//this should be a different view probably
		//called, main menu view or something...		
		if (views.getTopView().getName()== "MainMenu"){
			//Check for menu Change, then Update.
			if (men.getSelectedOption()){
				if(men.getSelectedIndex() == 0){
					men.select();
					gameTime.init(cam);
					views.addToList(gameTime);
					//views.getNested(gameTime.views);
				}
				else if(men.getSelectedIndex() == 1){
					men.select();
					editMenu.init();
					views.addToList(editMenu);
					//views.getNested(editMenu.views);
				}
				else if(men.getSelectedIndex() == 2){
					
				}
				else if(men.getSelectedIndex() == 3){

				}
				else if(men.getSelectedIndex() == 4){
					this.stop();
				}
			}
		}
		views.updateCurrentView(timePassed);
	}
	


	@Override
	public void draw(Graphics2D g) {
		g.clearRect(0, 0, s.getWidth(), s.getHeight());
		views.drawCurrentView(g);
	}


	
	//if the nested = 0 -> escape
	//if nested = 1 -> pop it
	//if nested = 2 -> pass it
	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		
		if(keyCode == KeyEvent.VK_ESCAPE){
			if (views.getNestedLevel() == 1){
				this.stop();
			}else if(views.getNestedLevel() == 2){ 
				views.removeTopView();				
			}else { 
				views.updateKeyEventPressed(e); 
			}
		} else {
			views.updateKeyEventPressed(e);
		}
				
	}

	@Override
	public void keyReleased(KeyEvent e) {
		views.updateKeyEventReleased(e);
	}


	@Override
	public void keyTyped(KeyEvent e) {
		views.updateKeyEventTyped(e);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		views.updateMouseClicked(e);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		views.updateMouseEntered(e);
	}

	@Override
	public void mouseExited(MouseEvent e) {
		views.updateMouseExited(e);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		views.updateMousePressed(e);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		views.updateMouseReleased(e);
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		views.updateMouseDragged(e);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		views.updateMouseMoved(e);
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		views.updateMouseWheelMoved(e);
	}

}
